using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public enum AnchorEdge
    {
        Left,
        Top,
        Right,
        Bottom
    }

    public class Anchor : IElementContainer
    {
        public AnchorEdge Edge = AnchorEdge.Left;

        private string parent = null;

        public string Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        private string anchor = null;

        public string AnchorTo
        {
            get { return anchor; }
            set { anchor = value; }
        }

        private string offset = null;

        public string Offset
        {
            get { return offset; }
            set { offset = value; }
        }

        public Anchor()
        { 
        }

        public Anchor(AnchorEdge edge)
        {
            Edge = edge;
        }

        public Anchor(Element element)
        {
            sourceElement = element;

            if (element.Name.ToLower() == "left") Edge = AnchorEdge.Left;
            else if (element.Name.ToLower() == "top") Edge = AnchorEdge.Top;
            else if (element.Name.ToLower() == "right") Edge = AnchorEdge.Right;
            else if (element.Name.ToLower() == "bottom") Edge = AnchorEdge.Bottom;

            Element subElement = element.FindFirstElement("parent", false);
            if (subElement != null)
            {
                parent = subElement.Value;
            }

            subElement = element.FindFirstElement("anchor", false);
            if (subElement != null)
            {
                anchor = subElement.Value;
            }

            subElement = element.FindFirstElement("offset", false);
            if (subElement != null)
            {
                offset = subElement.Value;
            }

        }

        #region IElementContainer Members

        private Element sourceElement;

        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element();

            switch (Edge)
            {
                case AnchorEdge.Left: sourceElement.Name = "left"; break;
                case AnchorEdge.Top: sourceElement.Name = "top"; break;
                case AnchorEdge.Right: sourceElement.Name = "right"; break;
                case AnchorEdge.Bottom: sourceElement.Name = "bottom"; break;
            }

            if (!String.IsNullOrEmpty(parent))
            {
                Element parentElement = new Element("parent", parent);
                sourceElement.ChildElements.Add(parentElement);
            }
            if (!String.IsNullOrEmpty(anchor))
            {
                Element anchorElement = new Element("anchor", anchor);
                sourceElement.ChildElements.Add(anchorElement);
            }
            if (!String.IsNullOrEmpty(offset))
            {
                Element offsetElement = new Element("offset", offset);
                sourceElement.ChildElements.Add(offsetElement);
            }
        }

        #endregion
    }
}
